﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace ManagedDLL
{
    public static class ObjectSerializer
    {
        public static byte[] Serialize(DataPackageObject dataobject)
        {
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();
                MemoryStream stream = new MemoryStream();
                formatter.Serialize(stream, dataobject);
                return stream.ToArray();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static DataPackageObject Deserialize(byte[] objectarray)
        {
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();
                MemoryStream stream = new MemoryStream(objectarray);
                stream.Position = 0;
                return (DataPackageObject)formatter.Deserialize(stream);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
